package com.jmoney.luckeylink.controller;

import static io.restassured.RestAssured.given;

import java.util.List;
import java.util.Map;

import com.jmoney.luckeylink.util.ExcelUtil;
import com.jmoney.luckeylink.util.HttpGetRequestUtil;

import io.restassured.RestAssured;
import io.restassured.config.SSLConfig;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;

public class LekeU_ApplyIC_Controller {
	/**
	 * <br>指定接口地址，获取登录用户的token信息</br>
	 * @author  刘智King
	 * @date     2018年4月20日 下午6:01:04
	 * @return token
	 */
    public static String GetToken() {
    	int TITLE_LINE_INDEX=6;
		ExcelUtil.getInstance().setFilePath("src/test/java/TestCasexls/JMoney.Luckeylink.Api.xlsm");
		ExcelUtil.getInstance().setSheetName("LekeU_Login");
		
        List<Map<String, String>> data = null;
        String PremiseUrl = ExcelUtil.getInstance().readExcelCell(1, 2);
        data = ExcelUtil.getInstance().readExcelAllData(TITLE_LINE_INDEX);
        Map<String, String> map = data.get(1-1);
        String type = map.get("type");
        String mobile = map.get("mobile");
        String captcha = map.get("captcha");
        
        String LoginUrl = PremiseUrl+"?"+"type="+type+"&mobile="+mobile+"&captcha="+captcha+"";
        String token = HttpGetRequestUtil.GetJsonDataStringValue(LoginUrl,"data.token");
        ExcelUtil.getInstance().setSheetName("LekeU_ApplyIC");
        System.out.println(token);
		return token;
    }
    
    /**
	    * <br>指定接口地址，获取登录用户的信息</br>
	    * @param ApiUrl
	    * @return UserDetail
	    */
    public static Object[] GetApplyICInfo(String ApiUrl,String Param) throws Exception{

	       Response response = given()
	               .config((RestAssured.config().sslConfig(new SSLConfig().relaxedHTTPSValidation())))
	               .contentType("application/x-www-form-urlencoded; charset=UTF-8")
	               .log().all()
	               .request()
	               .body(Param)
	               .when()
	               .post(ApiUrl);

	       response.print();
           String Json = response.asString();
	       
	       int status_code = response.jsonPath().getInt("status_code");
	       String message = response.jsonPath().get("message");
	       
	       String community_id = response.jsonPath().get("data.community_id");
	       String community_unit_id = response.jsonPath().get("data.community_unit_id");
	       String community_unit_room_id = response.jsonPath().get("data.community_unit_room_id");
	       JsonPath jsonPath = new JsonPath(Json).setRoot("data");
	       String id = jsonPath.getString("id");

           Object[] ApplyICInfo ={Json,status_code,message,community_id,community_unit_id,community_unit_room_id,id};
           
	       return ApplyICInfo;
	}   
}
